/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppDataOrderSaleSearchParam;
import com.jnzc.ocr.rest.app.shop.param.app.AppOrderSaleSearchParam;
import com.jnzc.ocr.serv.shop.api.*;
import com.jnzc.ocr.serv.shop.consts.ShopDictConsts;
import com.jnzc.ocr.serv.shop.consts.ShopOrderConsts;
import com.jnzc.ocr.serv.shop.entity.*;
import com.jnzc.ocr.serv.shop.filter.*;
import com.jnzc.ocr.serv.system.api.SysDictApi;
import com.jnzc.ocr.serv.system.vo.DictPair;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/17
 */
@Api(tags = {"app - 通用数据接口"})
@RestController
@RequestMapping("/app/data")
public class AppDataController extends BaseController {

    @Autowired
    private SysDictApi sysDictApi;

    @Autowired
    private ShopProductCategoryApi shopProductCategoryApi;

    @Autowired
    private ShopMemberCategoryApi shopMemberCategoryApi;

    @Autowired
    private ShopSupplierCategoryApi shopSupplierCategoryApi;

    @Autowired
    private ShopMemberApi shopMemberApi;

    @Autowired
    private ShopOrderApi shopOrderApi;

    @ApiOperation(value = "营销类型列表")
    @GetMapping("/marketingType")
    public List<DictPair> marketingType(){
        return sysDictApi.listPair(ShopDictConsts.SHOP_MARKETING_TYPE);
    }

    @ApiOperation(value = "会员获取方式列表")
    @GetMapping("/memberAccessType")
    public List<DictPair> memberAccessType(){
        return sysDictApi.listPair(ShopDictConsts.SHOP_MEMBER_ACCESS_TYPE);
    }

    @ApiOperation(value = "供应商获取方式列表")
    @GetMapping("/supplierAccessType")
    public List<DictPair> supplierAccessType(){
        return sysDictApi.listPair(ShopDictConsts.SHOP_SUPPLIER_ACCESS_TYPE);
    }

    @ApiOperation(value = "店铺分类列表")
    @GetMapping("/shopCategory")
    public List<DictPair> shopCategory(){
        return sysDictApi.listPair(ShopDictConsts.SHOP_CATEGORY);
    }

    @ApiOperation(value = "产品分类列表")
    @GetMapping("/productCategory")
    public List<ShopProductCategory> productCategory(){
        UserVo userVo = super.get();
        ShopProductCategoryFilter productCategoryFilter = new ShopProductCategoryFilter();
        productCategoryFilter.setShopId(userVo.getShopId());
        return shopProductCategoryApi.list(productCategoryFilter);
    }

    @ApiOperation(value = "会员分类列表")
    @GetMapping("/memberCategory")
    public List<ShopMemberCategory> memberCategory(){
        UserVo userVo = super.get();
        ShopMemberCategoryFilter memberCategoryFilter = new ShopMemberCategoryFilter();
        memberCategoryFilter.setShopId(userVo.getShopId());
        return shopMemberCategoryApi.list(memberCategoryFilter);
    }

    @ApiOperation(value = "供应商分类列表")
    @GetMapping("/supplierCategory")
    public List<ShopSupplierCategory> supplierCategory(){
        UserVo userVo = super.get();
        ShopSupplierCategoryFilter supplierCategoryFilter = new ShopSupplierCategoryFilter();
        supplierCategoryFilter.setShopId(userVo.getShopId());
        return shopSupplierCategoryApi.list(supplierCategoryFilter);
    }

    @ApiOperation(value = "会员搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "手机号/姓名", paramType = "query")
    })
    @GetMapping("/member/search")
    public PageResult<ShopMember> memberSearch(String keyword, Page page){
        UserVo userVo = super.get();
        ShopMemberFilter filter = new ShopMemberFilter();
        // filter.setSelectBy("id, name");
        filter.setKeyword(keyword);
        filter.setShopId(userVo.getShopId());
        return shopMemberApi.search(filter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "销售记录")
    @GetMapping("/orderSale/search")
    public PageResult<ShopOrder> orderSaleSearch(AppDataOrderSaleSearchParam param, Page page){
        UserVo userVo = super.get();
        ShopOrderFilter filter = new ShopOrderFilter();
        filter.setShopId(userVo.getShopId());
        filter.setType(ShopOrderConsts.TYPE_SALE);
        filter.setState(ShopOrderConsts.STATE_FINISH);
        filter.setBeginFinishTime(param.getBeginFinishTime());
        filter.setEndFinishTime(DateUtils.addDay(param.getEndFinishTime(), 1));
        filter.setOrderBy("finish_time desc");
        return shopOrderApi.search(filter, page.getPageNo(), page.getPageSize());
    }
}
